package ImageFileSet
{
	/*
/uploadsfolder (mine is C:\DP\uploads )
    /25xxx (the directory we download the raw images into)
        /25xxx (you create this one, and the next one too)
            /25xxx-page-images (this contains your output files)
    /25xxy (same deal, next project)
        /25xxy
            /25xxy-page-images
	*/
	
    import ImageFile.ImageFile;
    
    import com.adobe.serialization.json.JSON;
    
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.ProgressEvent;
    import flash.filesystem.File;
    import flash.filesystem.FileMode;
    import flash.filesystem.FileStream;
    
    import mx.collections.ArrayCollection;
    
    [Event(name=ProgressEvent.PROGRESS, type="flash.events.ProgressEvent")]
    [Event(name=ProgressEvent.COMPLETE, type="flash.events.ProgressEvent")]
    
	public class ImageFileSet extends EventDispatcher
	{
		private var _dir:File;
		private var _files:ArrayCollection;
		
		private var jsonRead:String;
		private var jsonWrite:String;
		
		public function ImageFileSet(dir:File)
		{
            _dir = dir;
            loadFilenames();
            loadMap();
		}
		
        public function get exists():Boolean {
        	return _files !== null && _files.length > 0;
        }
        
        public function getBytesTotal():Number {
        	var isum:uint = 0;
        	if(! _files || _files.length === 0) {
        		return 0;
        	}
        	else {
        		for(var i:uint = 0; i < _files.length; i++) {
        			isum += _files[i].size;
        		}
        		return isum;
        	}
        }
        
        public function deleteMap():void {
        	if(mapFile.exists) {
            	mapFile.deleteFile();
            }
        }
        private function get mapFile():File {
        	var s:String = ImageFile.ImageFile.buildPath(_dir.nativePath, "imagemap");
        	return File.applicationStorageDirectory.resolvePath(s);
        }
        
        public function saveMap():void {
            if(_files === null || _files.length === 0) {
            	if(mapFile.exists)
                    mapFile.deleteFile();
            	return;
            }
//                var s:String = JSON.encode(this.serialObject());
//                _files = JSON.decode(s);
                jsonWrite = JSON.encode(this._files);
                var fs:FileStream = new FileStream();
                fs.open(mapFile, FileMode.WRITE);
                fs.writeObject(jsonWrite);
                fs.close();
        }
        
       public function loadMap():void {
            if(mapFile.exists) {
                var stream:FileStream = new FileStream();
                stream.open(mapFile, FileMode.READ);
                jsonRead = stream.readObject() as String;
                stream.close();
                var j:* = JSON.decode(jsonRead);
//                this._files = j;
//                _files = new ArrayCollection();
//                var j:* = JSON.decode(jsonRead);
                for(var i:uint = 0; i < j.list.source.length; i++) {
                	mapItem(j.list.source[i]);
                }
            }
        }
        
//        private function serialObject():Object {
//            
//            var o:Object = new Object();
//            for(var i:uint = 0; i < this.imageFiles.length; i++) {
//                o[i] = this.imageFiles.getItemAt(i);
//            }
//            return o;
//        }
        
        private function getItemByName(str:String):ImageFile {
            var o:Object;
            for each(o in _files) {
            	if(o.name == str) {
            		return ImageFile (o);
            	}
            }	
            return null;
        }
        
        private function mapItem(o:Object):void {
        	var f:ImageFile = this.getItemByName(o.name);
        	if(f) {
        		f.section = o.section;
        		f.pageNumber = o.pageNumber;
        		f.species = o.species;
        		f.remark = o.remark;
        	}
        }
        
        public function getItem(idx:uint):ImageFile {
        	return ImageFile (_files.getItemAt(idx));
        }
        
        public function get imageFiles():ArrayCollection {
        	return _files;
        }
        
        [Bindable]
        public function set dataProvider(ac:ArrayCollection):void {
        	_files = ac;
        }

        public function get dataProvider():ArrayCollection {
        	return _files;
        }
        
        public function get length():uint {
        	return _files.length;
        }
        public function get path():String {
        	return _dir.nativePath;
        }
        
        private function loadFilenames():void {
	            var flist:Array = _dir.getDirectoryListing();
	            _files = new ArrayCollection();
	
//	            _bytesum = 0;
	            for(var i:int = 0; i < flist.length; i++) {
	               var f:File = File(flist[i]);
	               if(f.extension === "png" || f.extension === "jpg") {
	                   _files.addItem(new ImageFile(flist[i]));
//	                   _bytesum += f.size;
	               }
	            }
        }
        
        public function getItemIndex(imgfile:ImageFile):Number {
        	return _files.getItemIndex(imgfile);
        }
        
        public function RenameFiles(andText:Boolean=false):void {
//        	_bytesloaded = 0;
//            dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS, true, false, _bytesloaded, _bytesum));
        	for(var i:uint=0; i < _files.length; i++) {
        		var r:ImageFile = getItem(i);
        		r.copyRename(andText);
//        		_bytesloaded += r.size;
//        		dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS, false, false, _bytesloaded, _bytesum));
        	}
//        	dispatchEvent(new Event(Event.COMPLETE));
        }
    }
}    

